<template>
   <div id="Sku">
      <v-tableTitle
          :btnConfig="btnConfig"
          :tableSelectData="tableSelectData"
          @del="del"
          @newCreate="newCreate"
      ></v-tableTitle>
      <v-table
          :height="'270px'"
          :searchFlag="false"
          :page="page"
          :pageSize="pageSize"
          :tableConfig="tableConfig"
          :tableData="tableData"
          :total="total"
          @findData="getTableData"
          @getTableData="getTableData"
          @searhData="searhData"
          @selectData="selectData"
          @setdialog="setdialog"
          @upData="getTableData"
          ref="table"
      ></v-table>
   </div>
</template>

<script>
import table from "commons/Table";
import baseMixin from "@/mixins/BaseMixin";
import tableTitle from "@/components/commons/TableTitle";

export default {
   name: "replace",
   mixins: [baseMixin],
   props: {
      inputData: {
         type: Object
      }
   },
   data() {
      return {
         deleteStatus: true,
         loading: false,
         tableData: [],
         pageSize: 10,
         page: 1,
         total: 10,
         tableSelectData: [],
         tableSearhData: {},
         tableConfig: [
            {
               label: this.$t('config.shipper'),
               prop: "storerkey",
               input: "input",
            },
            {
               label: this.$t('config.sku'),
               prop: "sku",
               input: "input"
            },
           /* {
               label: "货品包装",
               prop: "packkey",
               input: "input"
            },*/
            {
               label: this.$t('outbound.replaceGoods'),
               prop: "substitutesku",
               input: "input",
            },
            {
               label: this.$t('outbound.uom'),
               prop: "uom",
               input: "input"
            },
            {
               label: this.$t('outbound.sequence'),
               prop: "sequence",
               input: "input"
            },
            {
               label: this.$t('outbound.qty'),
               prop: "qty",
               input: "input"
            },
            {
               label: this.$t('outbound.subqty'),
               prop: "subqty",
               input: "input",
            }
         ],
         addflag: true
      };
   },
   computed: {
      btnConfig() {
         if (this.$store.state.wms.toUpperCase().includes("WMWHSE")) {
           return [
              {label: this.$t('common.create'), certigier: this.newStatus, invalid: "newCreate"},
              {label: this.$t('common.delete'), certigier: this.deleteStatus, invalid: "del"},
           ];
        }else{
           return [];
        }
      }
   },
   methods: {
      newCreate() {
         this.$parent.fpkwFlag = true;
         this.$parent.setMentActive("12");
         this.$parent.detailDataFpkw = {
            storerkey: this.$parent.detailData.storerkey,
            sku: this.$parent.detailData.sku,
            qty: "",
            substitutesku: "",
            subqty: "",
            sequence: "",
            uom:"EA",
            subuom:"EA",
            uomqty:1,
            subuomqty:1,
            status:"0"
         };
      },
      async del(data) {
         try {
            let res = await this.$api.post(`/deleteSubstitutesku`, data);
            
               this.$message.success(res.returnMgr);
               this.getTableData(1, {});
               this.$refs.table.delSelectData(this.tableSelectData);
            
         } catch (e) {
            this.$message.error(e);
         }

      },
      //选中的内容
      selectData(data) {
         this.tableSelectData = data;
      },
      //搜索的值
      searhData(data, up) {
         if (!up) {
            this.tableSearhData = data;
         } else {
            this.tableSearhData = data;
            this.getTableData(1, this.tableSearhData);
         }
      },
      //设置弹框
      setdialog(data) {
         this.$api.post(`/querySubstituteskuByKey`, {
            substitutesku: data.substitutesku,
            sku: data.sku,
            storerkey: data.storerkey,
         }).then(res => {
            
               this.$parent.fpkwFlag = false;
               this.$parent.setMentActive("12");
               this.$parent.detailDataFpkw = {
                  ...res.bean
               };
         }).catch(e => {
            this.$message.error(e);
         })
      },
      async getTableData(page, data) {
         try {
            this.loading = true;
            let res = await this.$api.post(`/querySubstitutesku`, {
               curpage: page,
               sku: this.inputData.detailData.sku,
               storerkey: this.inputData.detailData.storerkey
            });
            
               this.tableData = res.page.list;
               this.page = res.page.curpage;
               this.pageSize = res.page.pageSize;
               this.total = res.page.total;
            
         } catch (e) {
            this.$message.error(e);
         } finally {
            this.loading = false;
         }
      }
   },
   watch: {
      tableSelectData(data) {
         if (data.length > 0) {
            this.deleteStatus = false;
         } else {
            this.deleteStatus = true;
         }
      }
   },
   mounted() {
      console.log(1);
      this.getTableData(1, {});
   },
   components: {
      "v-table": table,
      "v-tableTitle": tableTitle
   }
};
</script>

<style scoped>
#Sku {
   width: 100%;
   height: 99%;
   padding-top: 6px;
}
</style>
